<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupmanageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="序号" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.$index + 1 }} &lt;!&ndash; 使用 $index 获取当前行的索引，并加1以从1开始计数 &ndash;&gt;-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="拼团ID" align="center" prop="id" />
      <el-table-column label="拼团标题" align="center" prop="name" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="handleUpdate(scope.row)" >{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="几人团" align="center" prop="people">
        <template slot-scope="scope">
          {{ scope.row.people == 0 ? '三人团' : scope.row.people == 1 ? '五人团' : scope.row.people == 2 ? '十人团' : scope.row.people == 3 ? '二十人团' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="当前拼团总人数" align="center" prop="totalGroup" />
      <el-table-column label="每人限购" align="center" prop="limitBuy" />
      <el-table-column label="提货时间" align="center" prop="pickUpTime" />
      <el-table-column label="零售价格" align="center" prop="price" />
      <el-table-column label="拼团价格" align="center" prop="groupPrice" />
      <el-table-column label="已提货" align="center" prop="itemStatus0" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="itemstatus0(scope.row)" >{{scope.row.itemStatus0}}</span>
        </template>
      </el-table-column>
      <el-table-column label="未提货" align="center" prop="itemStatus1" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="itemstatus1(scope.row)" >{{scope.row.itemStatus1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属店铺" align="center" prop="storeName" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="store(scope.row)" >{{scope.row.storeName}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="审核状态  0审核通过、1进行中,2已结束,3已停止，4内容违规，5等待开始" align="center" prop="status" />-->
      <el-table-column label="开始时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{s}:{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{s}:{m}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="封面图" align="center" prop="surfacePlot" />-->
<!--      <el-table-column label="配图1" align="center" prop="firstImg" />-->
<!--      <el-table-column label="配图2" align="center" prop="secondImg" />-->
<!--      <el-table-column label="商品说明" align="center" prop="remark" />-->
<!--      <el-table-column label="邀请码" align="center" prop="invitationCode" />-->

<!--      <el-table-column label="可成几团" align="center" prop="maxGroup" />-->
      <el-table-column label="拼团状态" align="center" prop="groupStatus" >
<!--         0进行中、1已结束、2未开始、3已停止    -->
        <template slot-scope="scope">
          {{ scope.row.groupStatus == 0 ? '进行中' : scope.row.groupStatus == 1 ? '已结束' : scope.row.groupStatus == 2 ? '未开始' : scope.row.groupStatus == 3 ? '已停止' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="updatea(scope.row.id,scope.row.groupStatus)"
            v-hasPermi="['system:groupmanage:edit']">
            编辑
          </button>
        </template>
      </el-table-column>
    </el-table>

    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[1]"
        background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChangea"
        @current-change="handleCurrentChangea"
      />
    </div>

    <!-- 添加或修改拼团管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="拼团状态" prop="people">
          <el-select
            v-model="value"
            clearable
            placeholder="Select"
            style="width: 240px"
          ><el-option
              v-for="item in options"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="Axaya" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--        图片展示封面图 使用img标签-->
        <template>
          <img :src="form.surfacePlot" alt="" style="width: 200px;height: 150px">
        </template>
        <template>
          <img :src="form.firstImg" alt="" style="width: 200px;height: 150px"></img>
        </template>
        <template>
          <img :src="form.secondImg" alt="" style="width: 200px;height: 150px"></img>
        </template>
        <el-form-item label="商品说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入商品说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancels">取 消</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
import { listGroupmanage, getGroupmanage, delGroupmanage, addGroupmanage,updateGroupmanage ,itemGroup0} from "@/api/system/groupmanage";
import {GroupApp} from "@/api/system/groupa";

export default {
  name: "Groupmanage",
  computed: {
    scope() {
      return scope
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      value: null,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拼团管理表格数据
      groupmanageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      options:[{ id: 0, name: '进行中' },
        { id: 1, name: '已结束' },
        { id: 2, name: '未开始' },
        { id: 3, name: '已停止' }],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        people: null,
        totalGroup: null,
        pickUpTime: null,
        price: null,
        groupPrice: null,
        status: null,
        limitBuy: null,
        endTime: null,
        surfacePlot: null,
        firstImg: null,
        secondImg: null,
        invitationCode: null,
        name: null,
        maxGroup: null,
        groupStatus: null,
        leaderId:null,
        communityId:null,
        id:null
      },
      // 表单参数
      form: {},
      groupNamesss:"",
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.id = this.$route.query.Group;
    this.queryParams.name = this.$route.params.groupName;
    this.queryParams.leaderId = this.$route.params.leaderId;
    this.queryParams.communityId=this.$route.params.CommunityId;
    console.log(this.queryParams.leaderId+"  leaderId")
    if(this.queryParams.name!=null || this.queryParams.leaderId!=null || this.queryParams.communityId!=null){
      this.queryParams.pageNum=1;
      this.queryParams.pageSize=1000;
    }
    console.log(this.queryParams.name)
    this.getList();
  },
  methods: {
    /** 查询拼团管理列表 */
    getList() {

      if(this.queryParams.leaderId==null && this.queryParams.id==null){
        this.loading = true;
        listGroupmanage(this.queryParams).then(response => {
          this.groupmanageList = response.data.list;
          console.log(this.groupmanageList)
          // this.groupNamesss=this.groupmanageList.name
          // console.log(this.groupNamesss)
          this.total = response.data.total;
          this.loading = false;
          console.log("111")
          return ;
        });
      }
      if(this.queryParams.leaderId!=null){
        this.loading = true;
        itemGroup0(this.queryParams).then(response =>{
          this.groupmanageList = response.data.list;
          console.log(this.groupmanageList)
          // this.groupNamesss=this.groupmanageList.name
          // console.log(this.groupNamesss)
          this.total = response.data.total;
          this.loading = false;
          console.log("222")
          return ;
        })
      }
      if(this.queryParams.id!=null){
        this.loading = true;
        GroupApp(this.queryParams).then(res=>{
          this.groupmanageList = res.list;
          this.total = res.total;
          this.loading = false;
          console.log("333")
          return ;
        })
      }

    },
    //修改状态
    handleUpdateGroupStatus(){
      updateGroupmanage(this.form.id,this.value).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
        })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancels() {
      this.Axaya = false;
      this.reset();
    },
    handleSizeChangea(val) {
      this.queryParams.pageSize = val;
      this.getList(); // 假设这是你的数据获取函数
    },
    handleCurrentChangea(val) {
      this.queryParams.pageNum = val;
      this.getList(); // 同样地，假设这是你的数据获取函数
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        people: "",
        totalGroup: null,
        pickUpTime: null,
        price: null,
        groupPrice: null,
        status: null,
        createTime: null,
        limitBuy: null,
        endTime: null,
        surfacePlot: null,
        firstImg: null,
        secondImg: null,
        remark: null,
        invitationCode: null,
        name: null,
        maxGroup: null,
        groupStatus: ""
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      // this.handleQuery();
      this.queryParams.name=null;
      this.$route.params.groupName=null;
      window.location.reload();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拼团管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGroupmanage(id).then(response => {
        this.Axaya = true;
        this.form = response.data;
        this.title = "拼团介绍："+response.data.name+"(ID "+response.data.id+")";
      });
    },
    // 点击商铺根据商铺姓名进行查询
    store(row){
      this.$router.push({
        name: 'StoreMange',
        params: {
          storeName: row.storeName,
        }
      })
    },
    // 点击跳转到订单
    itemstatus0(row){
      this.$router.push({
        name: 'Ordera',
        params :{
          id:row.id,
          itemStatus0:0
        }
      })
    },
    itemstatus1(row){
      this.$router.push({
        name: 'Ordera',
        params :{
          id:row.id,
          itemStatus1:1
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            addGroupmanage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // //////////////
    // 弹框  下拉

    updatea(id,groupStatus){

      this.open = true;
      this.form.id=id
      this.form.groupStatus=groupStatus
      this.title = "编辑拼团："+this.groupNamesss;
      console.log(id,groupStatus)
    },


    /////////////////////////
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除拼团管理编号为"' + ids + '"的数据项？').then(function() {
        return delGroupmanage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/groupmanage/export', {
        ...this.queryParams
      }, `groupmanage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
